﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
#pragma warning disable CS8618

namespace Salary.Sys.Core.Configuration
{
    internal class ConfigurationDbContext: DbContext
    {
        private readonly string _databaseName;
        public ConfigurationDbContext(string databaseName="settings.db") : base()

        {
            _databaseName = databaseName;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite($"Filename={_databaseName}");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ConfigurationEntryModel>().HasKey(t => t.Key);

            modelBuilder.Entity<ConfigurationEntryModel>().Property(t => t.Key).IsRequired();
            modelBuilder.Entity<ConfigurationEntryModel>().Property(t => t.Domain).IsRequired();
            modelBuilder.Entity<ConfigurationEntryModel>().Property(t => t.Value).IsRequired();

            modelBuilder.Entity<ConfigurationEntryModel>().HasIndex(t => t.Domain).IsUnique(false);
        }

        public virtual DbSet<ConfigurationEntryModel> Entries { get; set; }
    }
}
